package beans;

import java.io.IOException;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.share.logging.ADFLogger;

import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.binding.OperationBinding;

import org.apache.commons.io.IOUtils;
import org.apache.myfaces.trinidad.model.UploadedFile;

import utils.ADFUtils;

public class SearchBean {

    private static final ADFLogger LOG = ADFLogger.createADFLogger("SearchBean");
    private byte[] bytes;
    private RichTable resultTable;

    public SearchBean() {
        super();
    }

    public void searchFish(ActionEvent actionEvent) {
        if (bytes != null) {
            OperationBinding search = ADFUtils.findOperation("searchSimmilarFishes");
            search.getParamsMap().put("bytes", bytes);
            search.execute();
            resultTable.setVisible(true);
        }
    }

    public void uploadFile(ValueChangeEvent event) {
        UploadedFile file = (UploadedFile)event.getNewValue();
        try {
            bytes = IOUtils.toByteArray(file.getInputStream());
        } catch (IOException e) {
            LOG.warning("Create bytes aborted");
        }
    }

    public void setResultTable(RichTable resultTable) {
        this.resultTable = resultTable;
    }

    public RichTable getResultTable() {
        return resultTable;
    }
}
